package com.tensquare.spit.controller;

import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import com.tensquare.spit.pojo.Spit;
import com.tensquare.spit.service.SpitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zzb
 * @version 1.0
 * @create 2020/12/17 11:45
 */
@RestController
@RequestMapping("spit")
public class SpitController {

    @Autowired
    private SpitService spitService;

    //查询所有
    @GetMapping
    public Result findAll(){
        List<Spit> spits = spitService.findAll();
        return new Result(true, StatusCode.OK,"查询所有成功",spits);
    }

    //增加吐槽
    @PostMapping
    public Result add(@RequestBody Spit spit){
        spitService.add(spit);
        return new Result(true,StatusCode.OK,"添加吐槽成功");
    }

    //根据id查询吐槽
    @GetMapping("{spitId}")
    public Result findById(@PathVariable String spitId){
        Spit spit = spitService.findById(spitId);
        return new Result(true,StatusCode.OK,"根据id查询吐槽成功",spit);
    }

    //修改吐槽
    @PutMapping("{spitId}")
    public Result update(@PathVariable String spitId,@RequestBody Spit spit){
        spitService.update(spitId,spit);
        return new Result(true,StatusCode.OK,"修改吐槽成功");
    }

    //删除吐槽(根据Id删)
    @DeleteMapping("{spitId}")
    public Result delete(@PathVariable String spitId){
        //根据Id删除吐槽
        spitService.delete(spitId);
        return new Result(true,StatusCode.OK,"删除吐槽成功");
    }

    //根据上级id查询吐槽列表并分页
    @GetMapping("comment/{parentid}/{page}/{size}")
    public Result comment(@PathVariable String parentid,@PathVariable Integer page,@PathVariable Integer size){
        PageResult<Spit> spitPageResult = spitService.comment(parentid,page,size);
        return new Result(true,StatusCode.OK,"根据上级id查询吐槽列表成功",spitPageResult);
    }

    //处理吐槽点赞
    @PutMapping("thumbup/{spitId}")
    public Result thumbup(@PathVariable String spitId){
        //处理点赞
        Boolean b = spitService.thumbup2(spitId);
        if(!b){
            return new Result(true,StatusCode.OK,"不能重复点赞");
        }
        return new Result(true,StatusCode.OK,"处理吐槽点赞成功");
    }
}
